package com.twu.biblioteca;

import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    private static final Biblioteca biblioteca = new Biblioteca();
    private static List<Menu> menus = biblioteca.getMenus();

    public static void main(String[] args) {
        printGreetings();
        boolean quit = false;
        while(!quit){
            printMainMenu();
            System.out.print("Please select a menu option:");
            int userSelectionIndex = userEnterSelection();
            boolean selectionValid = biblioteca.checkSelection(menus, userSelectionIndex);
            if(selectionValid){
                Menu userSelection = menus.get(userSelectionIndex-1);
                switch (userSelection){
                    case VIEW_BOOKS:{
                        printBookList();
                        if(checkIfUserLogined()){
                            selectBookFromTheList();
                        }
                        else{
                            System.out.println("After login, you can reserve the books.");
                        }
                    } break;
                    case CHECK_DETAILS: printQueryMembership();  break;
                    case QUIT: {
                        System.out.println("Bye!");
                        quit = true;
                    } break;
                    case VIEW_MOVIES: printMovieList();  break;
                    case LOGIN: handleUserLogin();  break;
                }
            }
            else{
                printInvalidNotify();
            }
        }

    }

    private static void printGreetings() {
        System.out.println(biblioteca.greeting());
        System.out.println(biblioteca.presentMovie());
    }

    private static boolean checkIfUserLogined() {
        return biblioteca.verifyUserStatus();
    }

    private static void handleUserLogin() {
        System.out.print("Please enter your username:");
        String username = userEnterInfo();
        if(biblioteca.checkIfUsernameExist(username)){
            System.out.print("Please enter your password:");
            String password = userEnterInfo();
            if(biblioteca.verifyIfPasswordIsCorrect(username, password)){
                User currentUser = biblioteca.getUserByUserName(username);
                biblioteca.setCurrentUser(currentUser);
                System.out.println("Welcome "+ username);
            }
            else {
                System.out.println("Login failed! Please check your password.");
            }
        }
        else{
            System.out.println("Username doesn't exist! Please check your username is correct or not.");
        }

    }

    private static String userEnterInfo() {
        InputStream inputStream = System.in;
        Scanner scanner = new Scanner(inputStream);
        String userEnterInfo = scanner.nextLine();
        return userEnterInfo;
    }

    private static void printMovieList() {
        List<Movie> movieList = biblioteca.getMovies();
        for(int i=0; i<movieList.size();i++){
            System.out.print("["+(i+1)+"] "+movieList.get(i).toString()+"\n");
        }
    }

    private static void selectBookFromTheList() {
        boolean selectValid = false;
        boolean available = false;
        while(!(available && selectValid)){
            System.out.print("Please select a book you like to reserve (select 0 to quit):");
            int selectedBookIndex = userEnterSelection();
            if(selectedBookIndex == 0){
                break;
            }
            List<Book> booksInLibrary = biblioteca.getBooks();
            selectValid = biblioteca.checkSelection(booksInLibrary,selectedBookIndex);
            if(selectValid){
                available = booksInLibrary.get(selectedBookIndex-1).ifBookIsAvailable();
            }
            if(available && selectValid){
                booksInLibrary.get(selectedBookIndex-1).changeBookStatus();
                System.out.println(biblioteca.handleReserveSuccess());
            }
            else{
                System.out.println(biblioteca.handleReserveFailure());
            }
        }
    }

    private static void printInvalidNotify() {
        System.out.println(biblioteca.notifyInvalidSelection());
    }

    private static void printMainMenu(){
        System.out.println("--------------------------------------------------------------------");
        for(Iterator<Menu> menuIterator = menus.iterator(); menuIterator.hasNext();){
            Menu menu = menuIterator.next();
            System.out.print((menu.ordinal()+1)+ ": " +menu.getDescription()+"\n");
        }
        System.out.println("--------------------------------------------------------------------");
    }

    private static void printBookList(){
        List<Book> bookList = biblioteca.getBooks();
        for(int i=0; i<bookList.size();i++){
            System.out.print("["+(i+1)+"] "+bookList.get(i).toString()+"\n");
        }
    }


    private static void printQueryMembership(){
        System.out.println(biblioteca.displayMembershipDetails(biblioteca.verifyUserStatus()));
    }

    private static int userEnterSelection(){
        InputStream is = System.in;
        int userSelection = 0;
        try{
            userSelection = biblioteca.getUserSelection(is);
        }catch (InputMismatchException e){
        }

        return userSelection;
    }
}
